﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.IO;
using System.Windows.Media;
using QQHelper;

namespace QQLogin
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private string _QQPwd;
        private string _QQVerfiy;
        private string _QQCode;
        public MainWindow()
        {
            InitializeComponent();
            Global.Cookie = new CookieContainer();
            txt_QQName.TextChanged += new TextChangedEventHandler(txt_QQName_TextChanged);
            this.KeyDown += new KeyEventHandler(MainWindow_KeyDown);
            if (txt_QQName.Text.Trim() != string.Empty)
            {
                Global.QQNum = txt_QQName.Text.Trim();
                SetVerfiyCode();
            }
        }

        void txt_QQName_TextChanged(object sender, TextChangedEventArgs e)
        {
            Global.QQNum = txt_QQName.Text.Trim();
            SetVerfiyCode();
        }
        void MainWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                btn_Login_Click(sender, e);
        }
        private void btn_Login_Click(object sender, RoutedEventArgs e)
        {
            Global.QQNum = txt_QQName.Text.Trim();
            _QQPwd = txt_QQPwd.Text.Trim();
            if (sp_code.Visibility == Visibility.Visible)
            {
                _QQCode = txt_QQCode.Text.Trim();
                if (_QQCode == "")
                {
                    MessageBox.Show(this, "验证码为空！");
                    return;
                }
            }
            else
            {
                SetVerfiyCode();
                if (_QQCode == null)
                {
                    MessageBox.Show(this, "验证码为空！");
                    return;
                }
            }
            try
            {

                bool isLogin = QQHelper.HttpHelper.IsLogin(Global.QQNum, _QQCode, _QQPwd, Global.Cookie);
                if (isLogin)
                {
                    Global.Gtk = HttpHelper.GetG_TK(null, Global.QQNum, Global.Cookie);
                    this.Hide();
                    QQMain main = new QQMain();
                    main.Show();
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message);

            }
        }

        #region 设置验证码

        /// <summary>
        /// 获取图片
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private ImageSource GetImageSource(Stream str)
        {
            BitmapImage image = new BitmapImage();
            image.BeginInit();
            image.StreamSource = str;
            image.EndInit();
            return image;
        }
        /// <summary>
        /// 设置验证码
        /// </summary>
        private void SetVerfiyCode()
        {
            _QQCode = null;
            if (txt_QQName.Text.Trim() == null || txt_QQName.Text.Trim() == string.Empty)
                return;
            string code = QQHelper.HttpHelper.GetVerfiyCode(Global.QQNum, Global.Cookie);
            //得验证吗
            if (code.Contains("ptui_checkVC('0','"))     //不需要手动输入
            {
                code = code.Replace("ptui_checkVC('0','", "").Replace("'", "").Replace(")", "").Replace(";", "").Substring(0, 4);
                _QQCode = code;
                sp_code.Visibility = Visibility.Collapsed;
            }
            else if (code.Contains("ptui_checkVC('1',"))
            {
                sp_code.Visibility = Visibility.Visible;
                Stream str = QQHelper.HttpHelper.GetVerfycodeImage(Global.Cookie, Global.QQNum);
                img_v.Source = GetImageSource(str);

            }
        }

        #endregion

        private void btn_out_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void img_v_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SetVerfiyCode();
        }
    }

}
